﻿// Copyright 2016 Intel Corporation.

// The source code, information and material ("Material") contained herein is
// owned by Intel Corporation or its suppliers or licensors, and title to such
// Material remains with Intel Corporation or its suppliers or licensors. The
// Material contains proprietary information of Intel or its suppliers and
// licensors. The Material is protected by worldwide copyright laws and treaty
// provisions. No part of the Material may be used, copied, reproduced, modified,
// published, uploaded, posted, transmitted, distributed or disclosed in any way
// without Intel's prior express written permission. No license under any patent,
// copyright or other intellectual property rights in the Material is granted to
// or conferred upon you, either expressly, by implication, inducement, estoppel
// or otherwise. Any license under such intellectual property rights must be
// express and approved by Intel in writing.

// Include any supplier copyright notices as supplier requires Intel to use.

// Include supplier trademarks or logos as supplier requires Intel to use,
// preceded by an asterisk. An asterisked footnote can be added as follows:
// *Third Party trademarks are the property of their respective owners.

// Unless otherwise agreed by Intel in writing, you may not remove or alter this
// notice or any other notice embedded in Materials by Intel or Intel's suppliers
// or licensors in any way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Security;
using ExtensionMethods;
using System.Windows.Interop;
using PasswordManager;

namespace Password_manager
{
    /// <summary>
    /// Interaction logic for EditDialog.xaml
    /// </summary>
    public partial class EditDialog : Window
    {
        PasswordItemDetails pNewItem;
        PasswordManagerCore mgr;

        public EditDialog(PasswordManagerCore mgr_in, PasswordItemDetails pItem)
        {
            InitializeComponent();
            if (pItem!=null)
            {
                pNewItem = new PasswordItemDetails(pItem.index);
                this.txtEAccount.Text = pItem.Account;
                this.txtEURL.Text = pItem.URLstring;
                this.txtELogin.Text = pItem.Login;
                this.txtEPassword.Text = "***************************";

                pNewItem = pItem;
                mgr = mgr_in;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                pNewItem.Account = txtEAccount.Text;
                pNewItem.Login = txtELogin.Text;
                pNewItem.URLstring = txtEURL.Text;
                this.DialogResult = true;
            }
            catch
            {
                this.DialogResult = false;                
            }
        
        }

        public PasswordItemDetails RetriveEditAccount()
        {
            return pNewItem;
        }

        private void btnSet_Click(object sender, RoutedEventArgs e)
        {
            // This form takes care of saving the password
            EditPassword inputDialog = new EditPassword(pNewItem, mgr);
            if ( inputDialog.ShowDialog() != true )
            {
                MessageBox.Show("Couldn't save password: " + MainWindow.returnErrorcode(inputDialog.error));
            }
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper wih;
            Window wnd;
            IntPtr hptr;
            int rv = 0;

            wnd = Window.GetWindow(this);
            wih = new WindowInteropHelper(wnd);
            hptr = wih.Handle;

            rv = mgr.accounts_view_password(pNewItem.index, hptr);
            if (rv != PasswordManagerStatus.OK)
            {
                MessageBox.Show("Couldn't view password: " + MainWindow.returnErrorcode(rv));
                return;
            }
        }
    }
}
